package com.qf.im.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.im.aspect.annotation.AuthReq;
import com.qf.im.base.Codes;
import com.qf.im.dao.FriendRequestDao;
import com.qf.im.entity.FriendRequest;
import com.qf.im.entity.Friendship;
import com.qf.im.entity.vo.RequestHandlerVo;
import com.qf.im.entity.vo.RequestInfoVo;
import com.qf.im.exception.ServiceException;
import com.qf.im.service.FriendRequestService;
import com.qf.im.service.FriendshipService;
import com.qf.im.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 好友申请表(FriendRequest)表服务实现类
 *
 * @author makejava
 * @since 2023-10-30 14:17:05
 */
@Service("friendRequestService")
public class FriendRequestServiceImpl extends ServiceImpl<FriendRequestDao, FriendRequest> implements FriendRequestService {


    @Autowired
    private FriendshipService friendshipService;

    /**
     * 新增好友申请
     * @param entity
     * @return
     */
    @Override
    @AuthReq
    public boolean save(FriendRequest entity) {
        //获取申请者ID
        Integer uid = UserUtils.getUid();
        //和被申请者的ID比对
        if (entity.getToId().equals(uid)) {
            //不能添加自己为好友
            throw new ServiceException(Codes.NOT_ADD_SELF);
        }

        //判断是否已经发送过
        Long count = this.query().eq("req_id", uid).eq("to_id", entity.getToId())
                .eq("status", 0).count();
        if (count > 0) {
            //还有未处理的申请
            throw new ServiceException(Codes.NOT_REP);
        }

        //设置申请者ID
        entity.setReqId(uid);

        //保存申请信息
        return super.save(entity);
    }

    /**
     * 查询对我的所有申请 - 按申请时间降序
     * @return
     */
    @AuthReq
    @Override
    public List<RequestInfoVo> query2MyRequest() {
        //获取 我 的用户ID
        Integer uid = UserUtils.getUid();
        //查询我的申请
        List<RequestInfoVo> requestInfoVos = getBaseMapper().queryRequestInfo(uid);
        return requestInfoVos;
    }

    /**
     * 查询当前用户待处理的申请数量
     * @return
     */
    @Override
    @AuthReq
    public int queryMeRequestNum() {
        Integer uid = UserUtils.getUid();
        Long count = this.query().eq("to_id", uid)
                .eq("status", 0)
                .count();
        return count.intValue();
    }

    /**
     * 处理好友申请
     * @param requestHandlerVo
     * @return
     */
    @Override
    @AuthReq
    @Transactional
    public int requestHandler(RequestHandlerVo requestHandlerVo) {

        //1、直接修改申请的状态 update friend_request set status = ? where id = ?
        boolean update = this.update()
                .set("status", requestHandlerVo.getStatus())
                .eq("id", requestHandlerVo.getReqid())
                .update();

        if (!update) {
            //修改没成功
            throw new ServiceException(Codes.FAIL);
        }

        //首先判断处理的状态
        if (requestHandlerVo.getStatus() == 2) {
            //请求拒绝
            return 1;
        }

        //根据申请ID查询申请的记录
        FriendRequest friendReq = this.getById(requestHandlerVo.getReqid());

        //请求用户
        Integer uid = UserUtils.getUid();

        //请求通过，建立好友关系
        //建立的 被申请者 和 申请者之间的好友关系
        Friendship friendship = new Friendship();
        friendship.setUid(uid);
        friendship.setFid(friendReq.getReqId());
        friendship.setBeizhu(requestHandlerVo.getBeizhu());
        friendshipService.save(friendship);

        //建立申请者 和 被申请者之间的好友关系
        Friendship friendship2 = new Friendship();
        friendship2.setUid(friendReq.getReqId());
        friendship2.setFid(uid);
        friendship2.setBeizhu(friendReq.getBeizhu());
        friendshipService.save(friendship2);
        return 1;
    }
}

